<template>
  <div class="admin-home">
    <!-- 头部欢迎信息 -->
    <div class="welcome-section">
      <h1>欢迎回来，{{ loginUserName }}</h1>
      <p>今天是 {{ todayDate }}，祝您工作愉快！</p>
    </div>

    <!-- 数据统计卡片 -->
    <div class="stats-container">
      <el-card shadow="hover" class="stat-card" @click="handleCardClick('user')">
        <div class="stat-icon user-icon">
          <el-icon><User /></el-icon>
        </div>
        <div class="stat-info">
          <h3>客户总数</h3>
          <p class="stat-number">{{ statistics.userCount }}</p>
        </div>
      </el-card>
      
      <el-card shadow="hover" class="stat-card" @click="handleCardClick('emp')">
        <div class="stat-icon emp-icon">
          <el-icon><Avatar /></el-icon>
        </div>
        <div class="stat-info">
          <h3>员工总数</h3>
          <p class="stat-number">{{ statistics.empCount }}</p>
        </div>
      </el-card>
      
      <el-card shadow="hover" class="stat-card" @click="handleCardClick('car')">
        <div class="stat-icon car-icon">
          <el-icon><Tools /></el-icon>
        </div>
        <div class="stat-info">
          <h3>车辆总数</h3>
          <p class="stat-number">{{ statistics.carCount }}</p>
        </div>
      </el-card>
      
      <el-card shadow="hover" class="stat-card" @click="handleCardClick('maintenance/index')">
        <div class="stat-icon appointment-icon">
          <el-icon><Calendar /></el-icon>
        </div>
        <div class="stat-info">
          <h3>总预约</h3>
          <p class="stat-number">{{ statistics.appointmentCount }}</p>

        </div>
      </el-card>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions">
      <h2>快捷操作</h2>
      <div class="action-grid">
        <div class="action-card" @click="handleCardClick('dept')">
          <div class="action-icon dept-icon">
            <el-icon><OfficeBuilding /></el-icon>
          </div>
          <p>部门管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('emp')">
          <div class="action-icon emp-icon">
            <el-icon><Avatar /></el-icon>
          </div>
          <p>员工管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('job')">
          <div class="action-icon job-icon">
            <el-icon><Briefcase /></el-icon>
          </div>
          <p>职位管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('user')">
          <div class="action-icon user-icon">
            <el-icon><User /></el-icon>
          </div>
          <p>用户管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('car')">
          <div class="action-icon car-icon">
            <el-icon><Tools /></el-icon>
          </div>
          <p>车辆管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('maintenance/index')">
          <div class="action-icon maintenance-icon">
            <el-icon><Calendar /></el-icon>
          </div>
          <p>保养管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('repair/index')">
          <div class="action-icon repair-icon">
            <el-icon><Warning /></el-icon>
          </div>
          <p>维修管理</p>
        </div>
        <div class="action-card" @click="handleCardClick('feedback')">
          <div class="action-icon feedback-icon">
            <el-icon><Message /></el-icon>
          </div>
          <p>反馈管理</p>
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  User, Avatar, Tools, Calendar, OfficeBuilding, 
  Briefcase, Warning, Message, PieChart 
} from '@element-plus/icons-vue'
import { queryAllApi } from '@/api/user.js'
import { queryAllApi as queryAllEmpApi } from '@/api/emp.js'
import { queryAllApi as queryAllCarApi } from '@/api/car.js'


const router = useRouter()

// 模拟数据
const statistics = ref({
  userCount: 128,
  empCount: 35,
  carCount: 156,
  appointmentCount: 10
})

// 获取登录用户名
const loginUserName = ref('管理员')
const loginUser = JSON.parse(localStorage.getItem('loginUser'))
if (loginUser) {
  loginUserName.value = loginUser.name || '管理员'
}

// 获取当前日期
const todayDate = ref('')

onMounted(() => {
  const now = new Date()
  const options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' }
  todayDate.value = now.toLocaleDateString('zh-CN', options)
  
  // 调用API获取真实统计数据
  loadStatistics()
})

// 加载统计数据（实际项目中应该调用后端API）
const loadStatistics = async () => {
  try {
    // 获取所有用户列表以计算用户总数
    const userResponse = await queryAllApi()
    if (userResponse && userResponse.data) {
      statistics.value.userCount = userResponse.data.length
    }
    
    // 获取所有员工列表以计算员工总数
    const empResponse = await queryAllEmpApi()
    if (empResponse && empResponse.data) {
      statistics.value.empCount = empResponse.data.length
    }
    
    // 获取所有汽车列表以计算汽车总数
    const carResponse = await queryAllCarApi()
    if (carResponse && carResponse.data) {
      statistics.value.carCount = carResponse.data.length
    }

    
    console.log('加载统计数据完成' ,maintenanceResponse.data.length)
  } catch (error) {
    console.error('加载统计数据失败:', error)
    // 失败时保持原有的模拟数据
  }
}

// 处理卡片点击事件
const handleCardClick = (path) => {
  // 确保路径以'/admin/'开头
  if (!path.startsWith('/')) {
    path = '/' + path;
  } else if (!path.startsWith('/admin/')) {
    path = '/admin' + path;
  }
  router.push(path)
}
</script>

<style scoped>
.admin-home {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

/* 欢迎区域 */
.welcome-section {
  margin-bottom: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 30px;
  border-radius: 10px;
  color: white;
}

.welcome-section h1 {
  margin: 0 0 10px 0;
  font-size: 28px;
  font-weight: 500;
}

.welcome-section p {
  margin: 0;
  opacity: 0.9;
}

/* 统计卡片 */
.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin-right: 20px;
}

.user-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.emp-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.car-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
}

.appointment-icon {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  color: white;
}

.stat-info h3 {
  margin: 0 0 5px 0;
  font-size: 14px;
  color: #606266;
  font-weight: 400;
}

.stat-number {
  margin: 0 0 5px 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.stat-trend {
  font-size: 12px;
  color: #909399;
}

.trend-up {
  color: #f56c6c;
}

.trend-down {
  color: #67c23a;
}

.trend-equal {
  color: #909399;
}

/* 快捷操作区域 */
.quick-actions {
  margin-bottom: 30px;
}

.quick-actions h2 {
  margin: 0 0 20px 0;
  font-size: 20px;
  color: #303133;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 20px;
}

.action-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.action-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.action-card .action-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  margin: 0 auto 10px;
}

.dept-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.job-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
}

.maintenance-icon {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  color: white;
}

.repair-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.feedback-icon {
  background: linear-gradient(135deg, #fccb90 0%, #d57eeb 100%);
  color: white;
}

.report-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
  color: white;
}

.action-card p {
  margin: 0;
  color: #303133;
  font-size: 14px;
}

/* 系统区域 */
.system-section {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.system-info {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.system-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #ebeef5;
}

.system-item:last-child {
  border-bottom: none;
}

.system-item .label {
  color: #606266;
  font-size: 14px;
}

.system-item .value {
  font-size: 14px;
  font-weight: 500;
  color: #303133;
}

.status-normal {
  color: #67c23a;
}

.activity-content p {
  margin: 0;
  color: #606266;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .system-section {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .stats-container {
    grid-template-columns: 1fr 1fr;
  }
  
  .action-grid {
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  }
  
  .welcome-section h1 {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .stats-container {
    grid-template-columns: 1fr;
  }
  
  .action-grid {
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  }
  
  .action-card .action-icon {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }
  
  .welcome-section {
    padding: 20px;
  }
}
</style>